/**
 * Created by wangkang
 * Created date 2018/6/5
 * Description: http请求
 * LastUpdater: wangkang
 * LastUpdate Time: 2018/6/5
 */
'use strict'

// import Vue from 'vue'
import vue from '@vue'
import axios from 'axios'
// import VueAxios from 'vue-axios'
import util from '@util'

// vue.use(VueAxios, axios)

// const newServer = 'http://testfx.api.ypt56.net/api/'
// const newServer = 'http://fx.api.ypt56.net/api/'
let httpStr = document.location.protocol === 'https:' ? 'https:' : 'http:'
const newServer = httpStr + '//yufxapi.ypt56.net/api/' //预生产
// const newServer = httpStr + '//fx.api.ypt56.net/api/'
function setNewApi (api) {
  return newServer + api
}

// http request 拦截器
axios.interceptors.request.use(
  config => {
    config.headers = {
      'Content-Type': 'application/json'
    }
    if (config.url.split('/api/')[1] === 'findPassword' || config.url.split('/api/')[1] === 'sendVerificationCode') {
      const data = config.data
      delete data.un
      delete data.rtk
      delete data.uid
      config.data = data
    }
    config.data = JSON.stringify(config.data)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // token 失效，重新登陆
    const data = response.data
    if (data.status === 550) {
      vue.$router.push('/login')
      vue.$notify.error({
        title: '提示',
        message: data.msg
      })
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

// function paramToString (data) {
//   let res = ''
//   for (let key in data) {
//     res += `&${key}=${data[key]}`
//   }
//   return res.substr(1, res.length)
// }

// const http = {
//   async get (api, data) {
//     const res = await axios.get(setApi(`${api}?${paramToString(data)}`))
//     return res
//   },
//   async post (api, data) {
//     if (!data.client_id) { // 除登录接口外的公共参数
//       let user = util.getItem('$user_fx')
//       data.uid = user.uid
//       data.un = user.un
//       data.rtk = user.rtk
//     }
//     let config = {
//       headers: {
//         'Content-Type': 'application/x-www-form-urlencoded'
//       }
//     }
//     data = JSON.stringify(data)
//     // var qs = require('querystring')
//     const res = await axios.post(setApi(api), data, config)
//     console.log(data)
//     return res
//   }
// }

// export default http

export function newGet (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(setNewApi(url), params)
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function newPost (url, data = {}) {
  if (!data.client_id) { // 除登录接口外的公共参数
    let user = util.getItem('$user_fx')
    if (user === null) {
      // vue.$router.push({path: '/login'})
    } else {
      data.uid = user.uid
      data.un = user.un
      data.rtk = user.rtk
    }
  }
  return new Promise((resolve, reject) => {
    axios.post(setNewApi(url), data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}
